/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
* ComGLHoodIView.java
*
* Created on November 15, 2007, 4:36 PM
 */
package gui.genomemap.view.inputView;

import gui.genomemap.event.event.WorkerInputEvent;
import gui.genomemap.inputModel.ComGLHoodIModel;
import gui.genomemap.popup.UsedOrderPopup;
import gui.genomemap.view.inputView.options.ComGLHoodIOptions;

import javautil.swing.SwingUtil;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

/**
 *
 * @author  stewari1
 */
public class ComGLHoodIView extends InputView {

    /** Field description */
    private final ComGLHoodIOptions optionsDialog = new ComGLHoodIOptions(new javax.swing.JFrame(),
                                                        false);

    /** Field description */
    private JPopupMenu tablePopUp = null;


    // Variables declaration - do not modify//GEN-BEGIN:variables

    /** Field description */
    private javax.swing.JLabel jLabel1;

    /** Field description */
    private javax.swing.JLabel linkageGrp_JLabel;

    /** Field description */
    private javax.swing.JComboBox linkagegrp_JComboBox;

    /** Field description */
    private javax.swing.JButton options_JButton;

    /** Field description */
    private javax.swing.JTextField order_JTextField;

    /** Field description */
    private javax.swing.JButton run_JButton;

    /** Field description */
    private javax.swing.JCheckBox usedOrder_JCheckBox;


    // End of variables declaration//GEN-END:variables

    /** Creates new form ComGLHoodIView */
    public ComGLHoodIView() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */


    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        run_JButton          = new javax.swing.JButton();
        options_JButton      = new javax.swing.JButton();
        linkageGrp_JLabel    = new javax.swing.JLabel();
        linkagegrp_JComboBox = new javax.swing.JComboBox();
        jLabel1              = new javax.swing.JLabel();
        order_JTextField     = new javax.swing.JTextField();
        usedOrder_JCheckBox  = new javax.swing.JCheckBox();

        run_JButton.setText("Run");
        run_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                run_JButtonActionPerformed(evt);
            }

        });
        options_JButton.setText("Options");
        options_JButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                options_JButtonActionPerformed(evt);
            }

        });
        linkageGrp_JLabel.setText("Linkage Group");
        linkagegrp_JComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "1", "2", "3", "4", "5", "6", "7"
        }));
        jLabel1.setText("Enter Order");
        order_JTextField.setToolTipText("Enter \",\" seperated natural numbers");
        usedOrder_JCheckBox.setText("Choose Used Orders ?");
        usedOrder_JCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        usedOrder_JCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        usedOrder_JCheckBox.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usedOrder_JCheckBoxActionPerformed(evt);
            }

        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

        this.setLayout(layout);
        layout.setHorizontalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup().addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(linkageGrp_JLabel).addComponent(jLabel1)).addGap(16, 16, 16)
                        .addGroup(layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(linkagegrp_JComboBox, javax.swing.GroupLayout
                                .PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing
                                .GroupLayout.PREFERRED_SIZE)
                                    .addComponent(order_JTextField, javax.swing.GroupLayout
                                        .PREFERRED_SIZE, 160, javax.swing.GroupLayout
                                        .PREFERRED_SIZE)).addGap(60, 60, 60)
                                            .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout
                                                    .Alignment.LEADING)
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addComponent(options_JButton)
                                                                .addPreferredGap(javax.swing
                                                                    .LayoutStyle.ComponentPlacement
                                                                        .RELATED)
                                                                            .addComponent(run_JButton))
                                                                                .addComponent(usedOrder_JCheckBox))
                                                                                    .addContainerGap(44, Short
                                                                                        .MAX_VALUE)));
        layout.setVerticalGroup(layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(linkagegrp_JComboBox, javax.swing.GroupLayout
                                .PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing
                                .GroupLayout.PREFERRED_SIZE).addComponent(linkageGrp_JLabel)))
                                    .addGroup(layout.createSequentialGroup().addContainerGap()
                                        .addGroup(layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment
                                                .BASELINE).addComponent(options_JButton)
                                                    .addComponent(run_JButton)))).addGap(13, 13, 13)
                                                        .addGroup(layout
                                                            .createParallelGroup(javax.swing
                                                                .GroupLayout.Alignment.BASELINE)
                                                                    .addComponent(jLabel1)
                                                                        .addComponent(order_JTextField, javax
                                                                            .swing.GroupLayout
                                                                                .PREFERRED_SIZE, javax
                                                                                    .swing.GroupLayout
                                                                                        .DEFAULT_SIZE, javax
                                                                                            .swing.GroupLayout
                                                                                                .PREFERRED_SIZE)
                                                                                                    .addComponent(usedOrder_JCheckBox))
                                                                                                        .addContainerGap(42, Short
                                                                                                            .MAX_VALUE)));

    }    // </editor-fold>//GEN-END:initComponents

    /**
     * Method description
     *
     * @param evt description
     */
    private void usedOrder_JCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_usedOrder_JCheckBoxActionPerformed

        if (!usedOrder_JCheckBox.isSelected()) {
            if (tablePopUp != null) tablePopUp.setVisible(false);
        } else {

            if (tablePopUp == null) {


                // first time case
                tablePopUp = UsedOrderPopup.createPopup(getView());


                // add popup menu listener
                tablePopUp.addPopupMenuListener(new PopupMenuListener() {

                    public void popupMenuCanceled(PopupMenuEvent e) {}
                    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                        usedOrder_JCheckBox.setSelected(false);
                    }
                    public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}

                });


                // add cell double click listener to the table under the popup
                final JTable table = (JTable) tablePopUp.getClientProperty(UsedOrderPopup.TABLE);

                table.addMouseListener(new MouseAdapter() {

                    public void mousePressed(MouseEvent e) {

                        int x = e.getX();
                        int y = e.getY();

                        if (e.getClickCount() == 2) {

                            int col = table.getSelectedColumn();

                            if (col != 2) return;

                            int row = table.getSelectedRow();

                            ComGLHoodIView.this.setOrderString("" + table.getValueAt(row, col));
                        }
                    }

                });
            }

            tablePopUp.show(this, 100, 90);
        }

    }    // GEN-LAST:event_usedOrder_JCheckBoxActionPerformed

    String getOrderString() {
        return order_JTextField.getText();
    }

    public void setOrderString(String orderString) {
        order_JTextField.setText(orderString);
    }

    /**
     * Method description
     *
     * @param evt description
     */
    private void options_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_options_JButtonActionPerformed

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                optionsDialog.setLocationRelativeTo(null);
                optionsDialog.setVisible(true);
            }

        });

    }    // GEN-LAST:event_options_JButtonActionPerformed

    public void referesh() {

        if (getInputModel() == null) {

            setEnabledRunModel(true);
            linkagegrp_JComboBox.setSelectedItem("1");
            setOrderString("");

            return;
        }

        ComGLHoodIModel comGLHoodIModel = (ComGLHoodIModel) getInputModel();

        setOrder(comGLHoodIModel.getOrderSet());
        linkagegrp_JComboBox.setSelectedItem("" + comGLHoodIModel.getLinkageGroup());
        optionsDialog.setOptions(comGLHoodIModel);
    }

    /**
     * Method description
     *
     * @param evt description
     */
    private void run_JButtonActionPerformed(java.awt.event.ActionEvent evt) {    // GEN-FIRST:event_run_JButtonActionPerformed

        ComGLHoodIModel inputModel = new ComGLHoodIModel();


        // get linkage group
        inputModel.setLinkageGroup(
            Integer.parseInt((String) linkagegrp_JComboBox.getSelectedItem()));


        // get order
        String orderString = getOrderString();

        if (orderString.length() == 0) {

            SwingUtil.showErrorDialog(this, "The order text field is empty !");

            return;
        }

        String[] orders = orderString.split(",");

        if (orders.length % 2 != 0) {

            for (String val : orders) {

                inputModel.addOrder(Integer.parseInt(val));
            }

        } else {

            SwingUtil.showErrorDialog(this, "The number of markers should be odd");

            return;
        }


        // get other options from the options dialog
        optionsDialog.getOptions(inputModel);

        WorkerInputEvent runLHoodEvent = new WorkerInputEvent(this, WorkerInputEvent.COM_GLHOOD);

        runLHoodEvent.setInputModel(inputModel);
        fireWorkerInputEvent(runLHoodEvent);

    }    // GEN-LAST:event_run_JButtonActionPerformed

    public void setEnabledRunModel(boolean enabled) {
        run_JButton.setEnabled(enabled);
    }
}
